import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { Storage } from '@utils/methods'
import Login from '@views/SystemPage/Login/index.vue'

// RouteRecordRaw 路由类型声明
const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    // 正则匹配无效路径,重定向
    {
        path: '/:catch(.*)',
        redirect: '/404'
    },
    // 错误页
    {
        path: '/404',
        name: 'Error',
        component: () => import('@views/SystemPage/Error/404.vue')
    },
    {
        path: '/',
        name: 'Home',
        component: () => import('@/views/SystemPage/Home/index.vue'),
        children: [
            {
                path: '',
                name: 'Welcome',
                meta: {
                    name: '欢迎页'
                },
                component: () => import('@/views/SystemPage/Home/components/Welcome/index.vue')
            },
            {
                path: 'MemberManage',
                name: 'MemberManage',
                meta: {
                    name: '会员管理'
                },
                component: () => import('@/views/MemberManage/Framework.vue'),
                children: [
                    {
                        path: 'MemberList',
                        name: 'MemberList',
                        meta: {
                            name: '会员列表'
                        },
                        component: () => import('@/views/MemberManage/MemberList/index.vue')
                    },
                    {
                        path: 'MemberDetail',
                        name: 'MemberDetail',
                        meta: {
                            name: '会员详情'
                        },
                        component: () => import('@/views/MemberManage/MemberDetail/index.vue')
                    },
                    {
                        path: 'MemberAnalysis',
                        name: 'MemberAnalysis',
                        meta: {
                            name: '会员分析'
                        },
                        component: () => import('@/views/MemberManage/MemberAnalysis/index.vue')
                    },
                    {
                        path: 'MemberAttribute',
                        name: 'MemberAttribute',
                        meta: {
                            name: '会员属性'
                        },
                        component: () => import('@/views/MemberManage/MemberAttribute/index.vue')
                    }
                ]
            },
            {
                path: 'MemberLevel',
                name: 'MemberLevel',
                meta: {
                    name: '会员等级'
                },
                component: () => import('@/views/MemberLevel/Framework.vue'),
                children: [
                    {
                        path: 'LevelConfig',
                        name: 'LevelConfig',
                        meta: {
                            name: '等级配置'
                        },
                        component: () => import('@/views/MemberLevel/LevelConfig/index.vue')
                    },
                    {
                        path: 'LevelAnalyze',
                        name: 'LevelAnalyze',
                        meta: {
                            name: '等级分析'
                        },
                        component: () => import('@/views/MemberLevel/LevelAnalyze/index.vue')
                    }
                ]
            },
            {
                path: 'MemberOperation',
                name: 'MemberOperation',
                meta: {
                    name: '会员运营'
                },
                component: () => import('@/views/MemberOperation/Framework.vue'),
                children: [
                    {
                        path: 'JoinMember',
                        name: 'JoinMember',
                        meta: {
                            name: '入会礼'
                        },
                        component: () => import('@/views/MemberOperation/JoinMember/index.vue')
                    },
                    {
                        path: 'MemberDay',
                        name: 'MemberDay',
                        meta: {
                            name: '会员日'
                        },
                        component: () => import('@/views/MemberOperation/MemberDay/index.vue')
                    },
                    {
                        path: 'LevelGift',
                        name: 'LevelGift',
                        meta: {
                            name: '等级专享礼'
                        },
                        component: () => import('@/views/MemberOperation/LevelGift/index.vue')
                    },
                    {
                        path: 'MemberIncentives',
                        name: 'MemberIncentives',
                        meta: {
                            name: '会员激励'
                        },
                        component: () => import('@/views/MemberOperation/MemberIncentives/index.vue')
                    },
                    {
                        path: 'DirectionalCare',
                        name: 'DirectionalCare',
                        meta: {
                            name: '定向关怀'
                        },
                        component: () => import('@/views/MemberOperation/DirectionalCare/index.vue')
                    },
                    {
                        path: 'CompleteMeans',
                        name: 'CompleteMeans',
                        meta: {
                            name: '完善资料'
                        },
                        component: () => import('@/views/MemberOperation/CompleteMeans/index.vue')
                    }
                ]
            },
            {
                path: 'BrandPromotion',
                name: 'BrandPromotion',
                meta: {
                    name: '品牌推广'
                },
                component: () => import('@/views/BrandPromotion/Framework.vue'),
                children: [
                    {
                        path: '',
                        name: 'ArticleList',
                        meta: {
                            name: '推广文章列表'
                        },
                        component: () => import('@/views/BrandPromotion/ArticleList/index.vue')
                    },
                    {
                        path: 'ArticleDetail',
                        name: 'ArticleDetail',
                        meta: {
                            name: '推广文章详情'
                        },
                        component: () => import('@/views/BrandPromotion/ArticleDetail/index.vue')
                    }
                ]
            },
            {
                path: 'MarketingActivities',
                name: 'MarketingActivities',
                meta: {
                    name: '营销活动'
                },
                component: () => import('@/views/MarketingActivities/Framework.vue'),
                children: [
                    {
                        path: 'ActivitiesList',
                        name: 'ActivitiesList',
                        meta: {
                            name: '活动列表'
                        },
                        component: () => import('@/views/MarketingActivities/ActivitiesList/index.vue')
                    },
                    {
                        path: 'AddActivity',
                        name: 'AddActivity',
                        meta: {
                            name: '新增活动'
                        },
                        component: () => import('@/views/MarketingActivities/AddActivity/index.vue')
                    },
                    {
                        path: 'ActivitiesAnalyze',
                        name: 'ActivitiesAnalyze',
                        meta: {
                            name: '活动分析'
                        },
                        component: () => import('@/views/MarketingActivities/ActivitiesAnalyze/index.vue')
                    }
                ]
            },
            {
                path: 'GoodsCenter',
                name: 'GoodsCenter',
                meta: {
                    name: '商品中心'
                },
                component: () => import('@/views/GoodsCenter/Framework.vue'),
                children: [
                    {
                        path: 'GoodsCenterList',
                        name: 'GoodsCenterList',
                        meta: {
                            name: '商品列表'
                        },
                        component: () => import('@/views/GoodsCenter/GoodsCenterList/index.vue')
                    },
                    {
                        path: 'AddEditDetail',
                        name: 'AddEditDetail',
                        meta: {
                            name: '添加/编辑商品'
                        },
                        component: () => import('@/views/GoodsCenter/AddEditDetail/index.vue')
                    }
                ]
            },
            {
                path: 'ApiPage',
                name: 'ApiPage',
                meta: {
                    name: '接口测试'
                },
                component: () => import('@/views/ApiPage/index.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

/**
 * 路由守卫
 * */
router.beforeEach((to, from, next) => {
    // 如果前进页非登录页，且无 token 则退出登录
    if (to.path !== '/login' && !Storage.get('token')) {
        next('/login')
    } else {
        next()
    }
})

export default router
